<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="1200" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-form ref="ruleForm" :model="replyInfo" :rules="rules" label-width="100px">

					<el-collapse v-model="activeNames">
						<el-collapse-item title="工单详情" name="工单详情">

							<template #title>
								<div class="class_title">工单详情</div>
							</template>

							<el-form-item style="margin-top: 18px;" label="创建时间" prop="created_at">
								<el-input v-model="form.created_at" style="width: 400px;"></el-input>
							</el-form-item>

							<el-form-item label="问题类别" prop="category_id">
								<blCategory style="width: 400px;" v-model="form.category_id" model="Complaint" />
							</el-form-item>
							<el-form-item label="工单号" prop="complaint_no">
								<el-input v-model="form.complaint_no" style="width: 400px;"></el-input>
							</el-form-item>
							<el-form-item label="反馈电话" prop="complaint_contact_phone">
								<el-input v-model="form.complaint_contact_phone" style="width: 400px;"></el-input>
							</el-form-item>

							<el-form-item label="处理进度" prop="complaint_status">
								<el-radio-group v-model="form.complaint_status">
									<el-radio :label="'待处理'">待处理</el-radio>
									<el-radio :label="'处理中'">处理中</el-radio>
									<el-radio :label="'已完成'">已完成</el-radio>
									<el-radio :label="'已取消'">已取消</el-radio>
									<el-radio :label="'待评价'">待评价</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-collapse-item>


						<el-collapse-item title="沟通记录" name="沟通记录">

							<view>
								<view>
									&nbsp;
								</view>
								<el-timeline style="max-width: 600px;margin-left: 20px;">
									<el-timeline-item v-for="(item, index) in form.complaint_records" :key="index"
										:icon="item.icon" :type="item.type" :color="item.color" :size="item.size"
										:hollow="item.hollow" :timestamp="item.time">
										<view style="display: flex;flex-direction: column;gap: 15px;">
											<view>
												<el-tag type="primary">{{item.role=='user'?'用户':'官方'}}</el-tag>
												{{item.username}}
											</view>
											<view style="white-space: pre-line;">
												{{item.title}}
											</view>
											<view>
												<template v-if="item.role=='user'">
													{{ item.content }}
												</template>
												<template v-else>
													<view v-html="item.content"></view>
												</template>
											</view>
											<view>
												<template v-for="img in item.images">
													<el-image style="max-width: 188px; max-height: 188px;"
														preview-teleported hide-on-click-modal
														:src="img+'?x-oss-process=image/resize,w_180&t='"
														:preview-src-list="item.images" show-progress fit="contain"
														:z-index="19900501" />
												</template>
											</view>
										</view>

									</el-timeline-item>
								</el-timeline>
							</view>

							<template #title>
								<div class="class_title">沟通记录</div>
							</template>

						</el-collapse-item>


						<el-collapse-item title="回复" name="回复">

							<template #title>
								<div class="class_title">待回复</div>
							</template>

							<el-form-item style="margin-top: 18px;" label="处理进度" prop="complaint_status">
								<el-radio-group v-model="replyInfo.reply_status">
									<el-radio :label="'处理中'">处理中</el-radio>
									<el-radio :label="'已结案'">已结案</el-radio>
								</el-radio-group>
							</el-form-item>

							<el-form-item label="回复内容" prop="reply_content">
								<sc-editor v-model="replyInfo.reply_content" placeholder="请输入内容"
									:height="400"></sc-editor>
							</el-form-item>

							<el-form-item>
								<el-button :loading="isSaveing" @click="submit" type="primary">回复</el-button>
							</el-form-item>
						</el-collapse-item>


					</el-collapse>

					<div style="height:400px">
						&nbsp;
					</div>

				</el-form>
			</el-main>
		</el-container>
	</el-drawer>
</template>

<script>
	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				apiObj: {},
				addTemplate: {

				},
				shortcuts: [{
					text: "每天0点5分",
					value: "0 5 0 * * ?"
				}],
				activeNames: ['工单详情', '沟通记录', '回复'],
				props: {
					label: 'title',
					value: 'id',
					keyword: 'keyword'
				},
				activeTab: 'basic',
				data: {

				},
				loading: false,
				mode: "add",
				titleMap: {
					add: '工单处理',
					edit: '工单处理'
				},
				replyInfo: {
					id: "",
					reply_content: '',
					reply_status: "处理中",
				},
				form: {
					id: '',
					complaint_status: '',
					complaint_content: '',
					category_id: '',
				},
				rules: {
					reply_content: [{
						required: true,
						message: '请填写回复内容'
					}]
				},
				visible: false,
				isSaveing: false,
				apiObj: null,
			}
		},
		mounted() {

		},
		methods: {
			success(res) {
				return true;
			},
			//显示
			open(data) {

				this.apiObj = this.$parent.apiObj

				if (data) {
					this.mode = data;
				}
				this.visible = true;
				this.$nextTick(() => {
					if (this.mode == 'add') {
						//this.$refs.table.rowAdd()
					}
				})
				return this;
			},
			//表单提交方法
			submit() {

				this.replyInfo.id = this.form.id;

				this.$refs.ruleForm.validate(async (data) => {
					if (data) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'add') {
							res = await this.apiObj.create.post(this.replyInfo);
						} else {
							res = await this.apiObj.reply.post(this.replyInfo);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', data, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {

				this.loading = true

				if (!data.id) {
					if (data.category_id) {
						this.form.category_id = data.category_id
					}
					this.loading = false
					return;
				}

				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.apiObj.show.get(params)
					this.loading = false
					this.form = res.data
				}, 0)
			}
		}
	}
</script>

<style>
	.class_title {
		padding-left: 18px;
		font-size: 18px;
		background-color: #f4f9ff;
		text-align: left;
		width: 100%;
	}
</style>
